package main;

import image.ImageUtils;

import java.util.Date;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;



import control.Button;
import control.ControlCommand;
import control.GroupControl;
import control.TextButton;

public class ImageCanvas extends Canvas{
	Image img;
	//DrawingCanvas drawingCanvas;
	MainMidlet midlet;
	GroupControl buttons;
	public ImageCanvas(MainMidlet m,final DrawingCanvas drawingCanvas) {
		// TODO Auto-generated constructor stub
		this.midlet = m;
		setFullScreenMode(true);
		//this.drawingCanvas = drawingCanvas;
		img = drawingCanvas.getImage();
		buttons = new GroupControl(9);
		
		buttons.setControl(0, new TextButton("Save", 10, 330, 100, 30));
		buttons.get(0).setCommand(new ControlCommand() {
			public void Do() {
				midlet.saveDrawingCanvas();
			}
		});
		
		buttons.setControl(1, new TextButton("Export PNG", 130, 330, 100, 30));
		buttons.get(1).setCommand(new ControlCommand() {
			public void Do() {
				InputScreen t=new InputScreen(midlet,drawingCanvas);
				midlet.show(t);
			}
		});
		
		buttons.setControl(2, new TextButton("Background color", 10, 100, 200, 30));
		buttons.get(2).setCommand(new ControlCommand() {
			public void Do() {
				midlet.showColorCanvas(new ColorHandler() {
					public int getColor() {
						return DrawingCanvas.backgroundColor;
					}
					public void setColor(int newColor) {
						DrawingCanvas.backgroundColor = newColor;
					}
				});
			}
		});
		
		buttons.setControl(3, new TextButton("Grid color", 10, 130, 200, 30));
		buttons.get(3).setCommand(new ControlCommand() {
			public void Do() {
				midlet.showColorCanvas(new ColorHandler() {
					public int getColor() {
						return DrawingCanvas.gridColor;
					}
					public void setColor(int newColor) {
						DrawingCanvas.gridColor = newColor;
					}
				});
			}
		});
		
		final TextButton showGrid = new TextButton("Show grid (on)", 10, 160, 200, 30);
		if(DrawingCanvas.showGrid){
			showGrid.setText("Show grid (on)");
		}else{
			showGrid.setText("Show grid (off)");
		}
		buttons.setControl(4, showGrid);
		showGrid.setCommand(new ControlCommand() {
			public void Do() {
				DrawingCanvas.showGrid = !DrawingCanvas.showGrid;
				if(DrawingCanvas.showGrid){
					showGrid.setText("Show grid (on)");
				}else{
					showGrid.setText("Show grid (off)");
				}
			}
		});
		
		buttons.setControl(5, new TextButton("Replace color", 10, 220, 120, 30));
		buttons.get(5).setCommand(new ControlCommand() {
			public void Do() {
				drawingCanvas.replaceColor();
				midlet.showDrawingCanvas();
			}
		});
		
		buttons.setControl(6, new TextButton("Move image", 10, 250, 120, 30));
		buttons.get(6).setCommand(new ControlCommand() {
			public void Do() {
				midlet.showMovingCanvas();
			}
		});
		
		buttons.setControl(7, new TextButton("Back to draw", 10, 360, 120, 30));
		buttons.get(7).setCommand(new ControlCommand() {
			public void Do() {
				midlet.showDrawingCanvas();
			}
		});
		
		buttons.setControl(8, new TextButton("Menu", 130, 360, 120, 30));
		buttons.get(8).setCommand(new ControlCommand() {
			public void Do() {
				midlet.showMenuCanvas();
			}
		});
	}
	public ImageCanvas getInstance(){
		return this;
	}
	protected void pointerReleased(int x, int y) {
		buttons.pointerReleased(x, y);
		repaint();
	}
	protected void pointerPressed(int x, int y) {
		buttons.pointerPressed(x, y);
		repaint();
	}

	protected void pointerDragged(int x, int y) {
		buttons.pointerPressed(x, y);
		repaint();
	}
	public Image getImage(){
		return img;
	}
	protected void paint(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(0xff222222);
		g.fillRect(0, 0, 240, 400);
		g.setColor(DrawingCanvas.backgroundColor);
		g.fillRect(120-img.getWidth()/2, 50-img.getHeight()/2, img.getWidth(), img.getHeight());
		g.drawImage(img, 120-img.getWidth()/2, 50-img.getHeight()/2, 0);
		buttons.paint(g);
	}
	public void setColor(int newColor){
		DrawingCanvas.backgroundColor = newColor;
	}
	public int getColor(){
		return DrawingCanvas.backgroundColor;
	}
}
